package com.mp.netty;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @author jack
 * @Description: 用于检测channel的心跳handler
 * 继承ChannelInboundHandlerAdapter，从而不需要实现channelRead0方法
 */
public class HeartBeatHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        // 判断evt是否是IdleStateEvent（用于触发用户事件，包含 读空闲/写空闲/读写空闲 ）
        if (evt instanceof IdleStateEvent) {
            // 强制类型转换
            IdleStateEvent event = (IdleStateEvent) evt;

            switch (event.state()) {
                case READER_IDLE:
                    System.out.println("进入读空闲...");
                    break;
                case WRITER_IDLE:
                    System.out.println("进入写空闲...");
                    break;
                case ALL_IDLE:
                    System.out.println("进入读写空闲...");

                    System.out.println("当前channel关闭前,用户数量为: " + NettyHandler.users.size());
                    Channel channel = ctx.channel();
                    // 关闭无用的channel，以防资源浪费
                    channel.close();
                    System.out.println("channel关闭后,用户数量为: " + NettyHandler.users.size());
                    break;
                default:
            }
        }
    }
}
